package com.hainatong.shiphelper.mainhome.fragment.massage;



import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.GlobalConfig;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentRecyclerviewBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.mainhome.adapter.WarnMsgAdapter;

import com.hainatong.shiphelper.mainhome.entity.MsgBean;
import com.hainatong.shiphelper.mainhome.viewmodel.MsgViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.widget.recycleviewdriver.ListItemDecoration;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;


import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;

public class WarnMsgFragment extends MvvmLazyFragment<FragmentRecyclerviewBinding, MsgViewModel>
{
    private WarnMsgAdapter adapter;
    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
        adapter = new WarnMsgAdapter();
        viewDataBinding.refreshlayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        viewDataBinding.refreshlayout.setRefreshFooter(new ClassicsFooter(getActivity()));
        viewDataBinding.recyclerview.setLayoutManager( new LinearLayoutManager(getContext()));
        viewDataBinding.recyclerview.setHasFixedSize(true);
        viewDataBinding.recyclerview.addItemDecoration( new ListItemDecoration(SizeUtils.dp2px(10)));
        viewDataBinding.recyclerview.setAdapter( adapter );
        adapter.setOnItemClickListener((adapter1, view1, position) -> {
            MsgBean msgBean = adapter.getItem( position );
            ComFragmentActivity.startViewByParam( getActivity(),Constants.VIEW_WARN_MSG_DEATIL,GsonUtil.obj2json(msgBean));
        });
        viewDataBinding.refreshlayout.setOnRefreshListener( refreshLayout -> {
            viewModel.tryRefreshWarnMsg();
        } );
        viewDataBinding.refreshlayout.setOnLoadMoreListener( refreshLayout -> {
            viewModel.loadMoreWarnMsg();
        } );


    }

    @Override
    protected void lazyLoad()
    {
        // 查询 预警消息
        if(ObjectUtils.isNotEmpty(GlobalConfig.getInstance().getUserInfo()))
        {
            viewModel.queryWarnMsg( GlobalConfig.getInstance().getUserInfo().getUuid() );
        }

        // list 结果
        viewModel.getMsgListResult().observe(this,result -> {
            if( result.isSuccess() )
            {
                if( result.isRefresh() )
                {
                    viewDataBinding.refreshlayout.finishRefresh(true);
                    if( result.getData() != null )
                    {
                        adapter.setList(result.getData());
                    }
                }
                else
                {
                    viewDataBinding.refreshlayout.finishLoadMore(true);
                    if( result.getData() != null )
                    {
                        adapter.addData(result.getData());
                    }
                }
            }
            else
            {
                if( result.isRefresh() )
                {
                    //刷新时 加载失败
                    ToastUtils.showShort( result.getMsg() );
                }
                else
                {
                    if( result.isLoadMoreEmpty() )
                    {
                        viewDataBinding.refreshlayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });


    }
}
